package com.kancy.stream.rabbit.properties;

import org.springframework.amqp.core.Binding;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMessageProperties
 *
 * @author kancy
 * @date 2020/3/7 20:06
 */
public class RabbitMessageProperties {

    /**
     * 项目
     */
    private String project;

    /**
     * 交换机
     */
    private Map<String, ExchangeProperties> exchange = Collections.emptyMap();

    /**
     * 队列
     */
    private Map<String, QueueProperties> queue = Collections.emptyMap();

    /**
     * 绑定
     */
    private Map<String, BingProperties> binding = Collections.emptyMap();

    /**
     * 绑定队列
     */
    private Map<String, BindingQueueProperties> bindingQueue = Collections.emptyMap();

    /**
     * 渠道: key -> channelKey , value -> ChannelProperties
     *
     */
    private Map<String, ChannelProperties> channel = Collections.emptyMap();

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public Map<String, ExchangeProperties> getExchange() {
        return exchange;
    }

    public void setExchange(Map<String, ExchangeProperties> exchange) {
        this.exchange = exchange;
    }

    public Map<String, QueueProperties> getQueue() {
        return queue;
    }

    public void setQueue(Map<String, QueueProperties> queue) {
        this.queue = queue;
    }

    public Map<String, BingProperties> getBinding() {
        return binding;
    }

    public void setBinding(Map<String, BingProperties> binding) {
        this.binding = binding;
    }

    public Map<String, BindingQueueProperties> getBindingQueue() {
        return bindingQueue;
    }

    public void setBindingQueue(Map<String, BindingQueueProperties> bindingQueue) {
        this.bindingQueue = bindingQueue;
    }

    public Map<String, ChannelProperties> getChannel() {
        return channel;
    }

    public void setChannel(Map<String, ChannelProperties> channel) {
        this.channel = channel;
    }


    /**
     * ExchangeProperties
     *
     * @author kancy
     * @date 2020/3/7 20:44
     */
    public static class ExchangeProperties {
        /**
         * 交换机名称
         */
        private String name;
        /**
         * 交换机类型
         */
        private String type = "direct";
        /**
         * 持久化
         */
        private boolean durable = true;
        /**
         * 自动删除
         */
        private boolean autoDelete = false;
        /**
         * 交换机参数
         */
        private Map<String, Object> arguments = Collections.emptyMap();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public boolean isDurable() {
            return durable;
        }

        public void setDurable(boolean durable) {
            this.durable = durable;
        }

        public boolean isAutoDelete() {
            return autoDelete;
        }

        public void setAutoDelete(boolean autoDelete) {
            this.autoDelete = autoDelete;
        }

        public Map<String, Object> getArguments() {
            return arguments;
        }

        public void setArguments(Map<String, Object> arguments) {
            this.arguments = arguments;
        }
    }

    /**
     * QueueProperties
     *
     * @author kancy
     * @date 2020/3/7 20:40
     */
    public static class QueueProperties {

        /**
         * 死信交换机属性
         */
        public static final String DLX_EXCHANGE_NAME = "x-dead-letter-exchange";

        /**
         * 死信路由属性
         */
        public static final String DLX_ROUTING_KEY = "x-dead-letter-routing-key";

        /**
         * 队列消息有效期属性
         */
        public static final String X_MESSAGE_TTL = "x-message-ttl";

        /**
         * 队列名称
         */
        private String name;

        /**
         * 持久化
         */
        private boolean durable = true;

        /**
         * 独占
         */
        private boolean exclusive = false;

        /**
         * 自动删除
         */
        private boolean autoDelete = false;

        /**
         * 指定死信交换机
         * x-message-ttl
         */
        private Duration ttl;

        /**
         * 指定死信交换机
         * x-dead-letter-exchange
         */
        private String dlxExchangeName;

        /**
         * 指定死信路由
         * x-dead-letter-routing-key
         */
        private String dlxRoutingKey;

        /**
         * 队列参数
         */
        private Map<String, Object> arguments = new HashMap<>();

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isDurable() {
            return durable;
        }

        public void setDurable(boolean durable) {
            this.durable = durable;
        }

        public boolean isExclusive() {
            return exclusive;
        }

        public void setExclusive(boolean exclusive) {
            this.exclusive = exclusive;
        }

        public boolean isAutoDelete() {
            return autoDelete;
        }

        public void setAutoDelete(boolean autoDelete) {
            this.autoDelete = autoDelete;
        }

        public Duration getTtl() {
            return ttl;
        }

        public void setTtl(Duration ttl) {
            this.ttl = ttl;
        }

        public String getDlxExchangeName() {
            return dlxExchangeName;
        }

        public void setDlxExchangeName(String dlxExchangeName) {
            this.dlxExchangeName = dlxExchangeName;
        }

        public String getDlxRoutingKey() {
            return dlxRoutingKey;
        }

        public void setDlxRoutingKey(String dlxRoutingKey) {
            this.dlxRoutingKey = dlxRoutingKey;
        }

        public Map<String, Object> getArguments() {
            return arguments;
        }

        public void setArguments(Map<String, Object> arguments) {
            this.arguments = arguments;
        }

    }


    /**
     * BingProperties
     *
     * @author kancy
     * @date 2020/3/7 20:43
     */
    public static class BingProperties {
        /**
         * 目的地
         */
        private String destination;

        /**
         * 目的地类型
         */
        private Binding.DestinationType destinationType = Binding.DestinationType.QUEUE;

        /**
         * 绑定的交换机
         */
        private String exchangeName;

        /**
         * 路由
         */
        private String routingKey;

        /**
         * Bing参数
         */
        private Map<String, Object> arguments = Collections.emptyMap();

        public String getDestination() {
            return destination;
        }

        public void setDestination(String destination) {
            this.destination = destination;
        }

        public Binding.DestinationType getDestinationType() {
            return destinationType;
        }

        public void setDestinationType(Binding.DestinationType destinationType) {
            this.destinationType = destinationType;
        }

        public String getExchangeName() {
            return exchangeName;
        }

        public void setExchangeName(String exchangeName) {
            this.exchangeName = exchangeName;
        }

        public String getRoutingKey() {
            return routingKey;
        }

        public void setRoutingKey(String routingKey) {
            this.routingKey = routingKey;
        }

        public Map<String, Object> getArguments() {
            return arguments;
        }

        public void setArguments(Map<String, Object> arguments) {
            this.arguments = arguments;
        }
    }



    /**
     * BindingQueueProperties
     *
     * @author kancy
     * @date 2020/3/7 23:42
     */
    public static class BindingQueueProperties extends QueueProperties {
        /**
         * 绑定的交换机
         */
        private String exchangeName;

        /**
         * 路由
         */
        private String routingKey;

        public String getExchangeName() {
            return exchangeName;
        }

        public void setExchangeName(String exchangeName) {
            this.exchangeName = exchangeName;
        }

        public String getRoutingKey() {
            return routingKey;
        }

        public void setRoutingKey(String routingKey) {
            this.routingKey = routingKey;
        }
    }

}
